package com.java.system.common.handler;

/*import com.joyoung.cloud.security.common.constant.CommonConstants;
import com.joyoung.cloud.security.common.exception.BaseException;
import com.joyoung.cloud.security.common.exception.BaseSystemException;
import com.joyoung.cloud.security.common.exception.BaseonException;
import com.joyoung.cloud.security.common.exception.auth.ClientTokenException;
import com.joyoung.cloud.security.common.exception.auth.UserInvalidException;
import com.joyoung.cloud.security.common.exception.auth.UserTokenException;
import com.joyoung.cloud.security.common.msg.BaseResponse;
import com.joyoung.cloud.security.common.msg.Result;
import com.joyoung.cloud.security.common.msg.ResultGenerator;*/
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.stream.Collectors;

/**
 * Created by ace on 2017/9/8.
 */
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
    private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
/*
    @ExceptionHandler(ClientTokenException.class)
    public BaseResponse clientTokenExceptionHandler(HttpServletResponse response, ClientTokenException ex) {
        response.setStatus(403);
        logger.error(ex.getMessage(), ex);
        return new BaseResponse(ex.getStatus(), ex.getMessage());
    }

    @ExceptionHandler(UserTokenException.class)
    public BaseResponse userTokenExceptionHandler(HttpServletResponse response, UserTokenException ex) {
        response.setStatus(200);
        logger.error(ex.getMessage(), ex);
        return new BaseResponse(ex.getStatus(), ex.getMessage());
    }

    @ExceptionHandler(UserInvalidException.class)
    public BaseResponse userInvalidExceptionHandler(HttpServletResponse response, UserInvalidException ex) {
        response.setStatus(200);
        logger.error(ex.getMessage(), ex);
        return new BaseResponse(ex.getStatus(), ex.getMessage());
    }

    @ExceptionHandler(BaseException.class)
    public BaseResponse baseExceptionHandler(HttpServletResponse response, BaseException ex) {
        logger.error(ex.getMessage(), ex);
        response.setStatus(500);
        return new BaseResponse(ex.getStatus(), ex.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public BaseResponse otherExceptionHandler(HttpServletResponse response, Exception ex) {
        response.setStatus(500);
        logger.error(ex.getMessage(), ex);
        return new BaseResponse(CommonConstants.EX_OTHER_CODE, ex.getMessage());
    }

    @ExceptionHandler(BaseonException.class)
    public BaseResponse baseExceptionHandler(HttpServletResponse response, BaseonException ex) {
        logger.error(ex.getMessage(), ex);
        response.setStatus(401);
        return new BaseResponse(ex.getStatus(), ex.getMessage());
    }


    //处理验证路径中请求实体校验失败后抛出的异常
    @ExceptionHandler(BindException.class)
    public Result BindExceptionHandler(HttpServletResponse response, BindException ex) {
        logger.error(ex.getMessage(), ex);
        response.setStatus(500);
        String message =
                ex.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining());
        return ResultGenerator.genFailResult(CommonConstants.VALIDATED_EXCODE, message);
    }

    //处理请求参数格式错误 @RequestParam上validate失败后抛出的异常是javax.validation.ConstraintViolationException
    @ExceptionHandler(ConstraintViolationException.class)
    public Result ConstraintViolationExceptionHandler(HttpServletResponse response,
                                                      ConstraintViolationException ex) {
        logger.error(ex.getMessage(), ex);
        response.setStatus(500);
        String message =
                ex.getConstraintViolations().stream().map(ConstraintViolation::getMessage).collect(Collectors.joining());
        return ResultGenerator.genFailResult(CommonConstants.VALIDATED_EXCODE, message);
    }

    //处理请求参数格式错误 @RequestBody上validate失败后抛出的异常是MethodArgumentNotValidException异常。
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result MethodArgumentNotValidExceptionHandler(HttpServletResponse response,
                                                         MethodArgumentNotValidException ex) {
        logger.error(ex.getMessage(), ex);
        response.setStatus(500);
        String message =
                ex.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining());
        return ResultGenerator.genFailResult(CommonConstants.VALIDATED_EXCODE, message);
    }

    @ExceptionHandler(BaseSystemException.class)
    public Result BaseSystemExceptionHandle(HttpServletResponse response, BaseSystemException ex) {
        return ResultGenerator.genFailResult(ex.getCode(), ex.getMessage());
    }*/
}
